// pages/filter/secondary.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    initialData: [],//初始数据
    arrayData: [],
    selected: [],
    selectedText: "",
    selectedData: [],
    totalNum: 0,
    levelCount: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.setNavigationBarTitle({
      title: options.title || ""
    })
    that.data.initialData = JSON.parse(options.data) || []
    that.levelCount(options.id);
    that.setData({
      arrayData: that.dataGroup(that.data.initialData),
      levelCount: that.data.levelCount
    })
    //获取已选
    wx.getStorage({
      key: 'selected',
      success: function (res) {
        var totalNum = 0
        var selected = []
        that.data.initialData.forEach(function (item) {
          res.data.forEach(function (i) {
            if (i.id == item.id) {
              item.checked = true
              selected.push(item.attribute_name)
              totalNum++
            }
          })
        })
        that.setData({
          selectedText: selected.join("、"),
          totalNum: totalNum,
          selectedData: res.data,
          arrayData: that.dataGroup(that.data.initialData)
        });
      },
    })
  },
  filterData: function (id) {
    var that = this
    var array = []
    that.data.initialData.forEach(function (item) {
      if (item.parent_id == id) {
        array.push(item)
      }
    });
    return array;
  },
  levelCount: function (id) {
    var that = this
    var res = that.filterData(id);
    var index = 0
    res.forEach(function (item) {
      index++
      if (index == res.length) {
        that.data.levelCount++
        that.levelCount(item.id)
      }
    })
  },
  dataGroup: function (list, id, pid) {
    var temptree = [], tree = [], items = [];
    for (var i in list) {
      if (id == list[i].id) {
        list[i].checked = list[i].checked ? false : true
      } else if (pid) {
        if (pid == list[i].parent_id) {
          list[i].checked = false
        }
      }
      if (!temptree[list[i].id]) {
        list[i].child = []
        temptree[list[i].id] = list[i];
        items.push(list[i]);
      }
    }
    for (var j in items) {
      if (id == items[j].id) {
        // items[j].checked = items[j].checked ? false : true
      }
      if (temptree[items[j].parent_id]) {
        temptree[items[j].parent_id]['child'].push(temptree[items[j].id]);
      } else {
        tree.push(temptree[items[j].id]);
      }
    }
    temptree = null;
    items = null;
    return tree;
  },
  upData: function (id) {
    var that = this
    var res = that.filterData(id);
    var index = 0
    res.forEach(function (item) {
      index++
      if (item.id == id) {
        item.child()
      }
      if (index == res.length) {
        that.levelCount(item.id)
      }
    })
  },
  selected: function (e) {
    var that = this
    var slefData = e.currentTarget.dataset
    var data = that.dataGroup(that.data.initialData, slefData.id, slefData.parentid)//设置选中
    var selected = []
    var totalNum = 0;
    if (slefData.disabled > 0) return
    //选中之后的处理
    that.data.initialData.forEach(function (item) {
      if (item.checked) {
        var data = {}
        data.id = item.id
        data.pid = slefData.pid
        data.attribute_name = item.attribute_name
        selected.push(item.attribute_name)//选中显示
        if (item.id == slefData.id) {
          that.data.selectedData.push(data)
        }
        totalNum++
      } else {
        //这个判断为多选取消注释为多选
        // if (item.id == slefData.id){
        that.data.selectedData.forEach(function (item2, i) {
          if (item2.id == item.id) {
            that.data.selectedData.splice(i, 1)
          }
        })
        // }
      }
    })
    // that.data.selectedData.forEach(function (item) {
    //   selectedData.forEach(function (item2) {
    //     if (item.id==item2.id){

    //     }
    //   })
    // })
    that.setData({
      selectedText: selected.join("、"),
      arrayData: data,
      totalNum: totalNum,
    });
  },
  submitFiter: function () {
    var that = this
    wx.setStorage({
      key: 'selected',
      data: that.data.selectedData,
      success: function () {
        wx.navigateBack();
      }
    })
  }
})